package com.world.cat.controller.sys;

import com.world.cat.model.sys.Role;
import com.world.cat.service.generate.FieldService;
import com.world.cat.service.sys.MenuService;
import com.world.cat.service.sys.RoleMenuService;
import com.world.cat.service.sys.RoleResourceService;
import com.world.cat.service.sys.RoleService;
import com.world.common.pojo.Result;
import com.world.common.pojo.WorldPage;
import com.world.common.pojo.tree.ZTreeNode;
import com.world.common.util.NumberUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value = "/sys/role", name = "角色管理")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private RoleResourceService roleResourceService;

    @RequestMapping(value = "/home")
    public String home(Model model) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Role.class));
        model.addAttribute("qClassActions", FieldService.getClassActionInfo(Role.class));
        return "/sys/role/home";
    }

    @RequestMapping(value = "/edit", name = "编辑")
    public String edit(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Role.class));
        Role object = roleService.get(id);
        model.addAttribute("obj", object);
        return "/sys/role/edit";
    }

    @RequestMapping(value = "/add", name = "添加")
    public String add(Model model) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Role.class));
        return "/sys/role/add";
    }

    @RequestMapping(value = "/view")
    public String view(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Role.class));
        Role object = roleService.get(id);
        model.addAttribute("obj", object);
        return "/sys/role/view";
    }

    @RequestMapping(value = "/menu", name = "配置菜单")
    public String menu(Model model, String id) {
        model.addAttribute("role", roleService.getRole(id));
        return "sys/role/menu";
    }

    @RequestMapping(value = "/resource", name = "配置权限")
    public String resource(Model model, String id) {
        model.addAttribute("role", roleService.getRole(id));
        return "sys/role/resource";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String params, WorldPage worldPage, HttpServletRequest request) throws Exception {
        //jqxgrid是用这个值来传递page和row的
        int pagenum = NumberUtil.intValue(request.getParameter("pagenum")) + 1;
        int pagesize = NumberUtil.intValue(request.getParameter("pagesize"));
        worldPage.setPageIndex(pagenum);
        worldPage.setPageSize(pagesize);
        worldPage.setProperties("createTime");
        Page<Role> pages = roleService.findPage(Role.class, params, worldPage.getPageRequest());
        return pages;
    }

    @RequestMapping(value = "/del", name = "删除")
    @ResponseBody
    public Object del(String[] ids) {
        roleService.del(ids);
        return new Result(true);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(Role role) throws Exception {
        Result result = roleService.saveRole(role);
        return result;
    }

    @RequestMapping(value = "/menuTree")
    @ResponseBody
    public Object menuTree(String roleId) {
        ZTreeNode zTreeNode = roleMenuService.getRoleMenuTree(roleId);
        return zTreeNode.getChildren();
    }

    @RequestMapping(value = "/resourceTree")
    @ResponseBody
    public Object resourceTree(String roleId) {
        ZTreeNode zTreeNode = roleResourceService.getRoleResourceTree(roleId);
        return zTreeNode.getChildren();
    }

    @RequestMapping(value = "/updateRoleMenu")
    @ResponseBody
    public Object updateRoleMenu(String roleId, String[] addIds) {
        roleMenuService.updateRoleMenu(roleId, addIds);
        return new Result(true, "Success");
    }

    @RequestMapping(value = "/saveRoleResource")
    @ResponseBody
    public Object saveRoleResource(String roleId, String[] addIds) {
        roleResourceService.saveRoleResource(roleId, addIds);
        return new Result(true, "Success");
    }
}
